<?php

/**
 * home actions.
 *
 * @package    travide
 * @subpackage home
 * @author     Travide Project Group
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class homeActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $ciTable = Doctrine::getTable('CollectionItem');
        $q = $ciTable->addNonArchivedQuery();
        $q = $ciTable->addOrderByLastCreated($q);
        $q->limit(sfConfig::get('app_show_last_updated_count'));
        $this->lastUpdatedItemList = $q->execute();

            $q = Doctrine::getTable('SubjectTag')->addOrderByFrequencyQueryDesc();
            $q->limit(sfConfig::get('app_show_top_subject_tags_count'));
            $this->topSubjectTagsList = $q->execute();
       
    }

    public function executeError404(sfWebRequest $request) {

    }

    public function executeAbout(sfWebRequest $request) {

    }

    public function executeContactNew(sfWebRequest $request) {
        $this->form = new ContactForm();
    }

    public function buildContactMessageFromForm(ContactForm $contactForm, sfWebRequest $request) {
        $messageText = '<html><body>';
        $messageText .= '<h1>Contact Form Submission</h1>';
        $messageText .= '<h2>Message Content</h2>';
        $messageText .= '<table>';
        $messageText .= '<tr><th>Name</th><td>' . htmlspecialchars($contactForm->getValue('name')) . '</td></tr>';
        $messageText .= '<tr><th>Email</th><td>' . htmlspecialchars($contactForm->getValue('email')) . '</td></tr>';
        $messageText .= '<tr><th>Phone</th><td>' . htmlspecialchars($contactForm->getValue('phone')) . '</td></tr>';
        $messageText .= '<tr><th>Regarding</th><td>' . htmlspecialchars($contactForm->getValue('regarding')) . '</td></tr>';
        $messageText .= '<tr><th>Message</th><td>' . htmlspecialchars($contactForm->getValue('message')) . '</td></tr>';
        $messageText .= '</table>';
        $messageText .= '<h2>Message Tracing</h2>';
        $messageText .= '<table>';
        $messageText .= '<tr><th>IP Address</th><td>' . htmlspecialchars($request->getRemoteAddress()) . '</td></tr>';
        $messageText .= '<tr><th>HTTP Culture</th><td>' . htmlspecialchars($request->getPreferredCulture()) . '</td></tr>';
        $messageText .= '<tr><th>Script</th><td>' . htmlspecialchars($request->getScriptName()) . '</td></tr>';
        $messageText .= '<tr><th>Host</th><td>' . htmlspecialchars($_SERVER['HTTP_HOST']) . '</td></tr>';
        $messageText .= '</table>';
        $messageText .= '</body></html>';
        return $messageText;
    }

    public function executeContactCreate(sfWebRequest $request) {
        $this->form = new ContactForm();

        $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
        if ($this->form->isValid()) {
            /**
             * Send the email
             */
            $contactEmail = $this->getMailer()->compose(array(sfConfig::get('app_general_admin_email') => sfConfig::get('app_general_system_name') . ' Contact Form'), sfConfig::get('app_contact_email_address'));
            $contactEmail->setSubject(sfConfig::get('app_contact_email_subject'));
            $messageText = $this->buildContactMessageFromForm($this->form, $request);
            $contactEmail->setBody($messageText);
            $contactEmail->setContentType('text/html');
            $this->getMailer()->send($contactEmail);
            $this->getLogger()->log('EMAIL:Contact Form:' . $messageText);
        } else {
            $this->setTemplate('contactNew');
        }
    }

}
